
import android.content.Context
import android.net.ConnectivityManager
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * 作者：马海钊
 * 时间：2023/8/14 15:29
 * 功能：
 */
object HttpUtils {
    var httpUtils: HttpUtils ?= null
    var api: ApiService

    init {
        val retrofit = Retrofit.Builder()
            .baseUrl("http://10.59.9.18/")
            .client(OkHttpClient.Builder()
                .connectTimeout(50000,TimeUnit.SECONDS)
                .writeTimeout(50000,TimeUnit.SECONDS)
                .readTimeout(50000,TimeUnit.SECONDS)
                .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        api = retrofit.create(ApiService :: class.java)
    }
    @Synchronized
    fun getInstance(): HttpUtils {
        if (httpUtils == null) {
            httpUtils = HttpUtils
        }
        return httpUtils!!
    }
    fun isHttp(context: Context): Boolean {
        val conn = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val info = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
        return info != null && info.isConnected
    }

}